package com.zcx.qshop.conn;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.AsyType;
import com.zcx.helper.http.note.HttpInlet;
import com.zcx.qshop.entity.Good;

@HttpInlet(Conn.INLET_JSON_GOODSLIST)
public class JsonGoodslistAsyGet extends QSAsyGet<JsonGoodslistAsyGet.Info> {
	
	public String areaid, ftypeid, stypeid, ttypeid, serchtype, page;
 
	public JsonGoodslistAsyGet(String areaid, String ftypeid, String stypeid, String ttypeid, String serchtype, String page, AsyCallBack<Info> asyCallBack) {

		super(asyCallBack);

		this.areaid = areaid;

		this.ftypeid = ftypeid;
		
		this.stypeid = stypeid;
		
		this.ttypeid = ttypeid;
		
		this.serchtype = serchtype;
		
		this.page = page;

		ASY_TYPE = AsyType.MAX_STALE;
		
 	}
	
	@Override
	public Info parser(JSONObject object) {
		
		if(object.optString("message").equals("1")){
			
			Info info = new Info();
			
			info.nextpage = object.optString("nextpage");
			
			info.ispage = object.optString("ispage");
			
			JSONArray array = object.optJSONArray("ttypelist");
			
			for (int i = 0; i < array.length(); i++) {
				
				Info.Ttype ttype = new Info.Ttype();
				
				JSONObject o = array.optJSONObject(i);
				
				ttype.ttypeid = o.optString("ttypeid");
				
				ttype.title = o.optString("title");
				
				info.ttypes.add(ttype);
				
			}
			
			array = object.optJSONArray("goodslist");

			Info.GoodDouble goodDouble = new Info.GoodDouble();
			
			for (int i = 0; i < array.length(); i++) {
				
				Good good = new Good();
				
				JSONObject o = array.optJSONObject(i);
				
				good.pid = o.optString("gid");
				
				good.picurl = Conn.SERVICE + o.optString("picurl");
				
				good.title = o.optString("title");
				
				good.description = o.optString("description");
				
				good.number = o.optString("number");
				
				good.price = o.optString("price");
				
				good.unit = o.optString("unit");

				if(goodDouble.goodOne == null){

					goodDouble.goodOne = good;

					if(i == array.length() - 1){

						info.goodDoubles.add(goodDouble);

					}

				}else{

					goodDouble.goodTwo = good;

					info.goodDoubles.add(goodDouble);

					goodDouble = new Info.GoodDouble();

				}
				
			}
			
			return info;
			
		}
		
		return null;
		
	}

	public static class Info{
		
		public String nextpage, ispage;

		public List<Ttype> ttypes = new ArrayList<Ttype>();
		
		public List<GoodDouble> goodDoubles = new ArrayList<>();
		
		public static class Ttype{
			
			public String ttypeid, title;
			
		}

		public static class GoodDouble{

			public Good goodOne, goodTwo;

		}
		
	}

}
